import 'package:flutter/material.dart';
import 'package:home/assets/r.dart';
import 'package:home/net/base_response_entity.dart';
import 'package:home/net/network.dart';
import 'package:home/utils/loger.dart';

class RegisterPage extends StatefulWidget {
  const RegisterPage({Key? key}) : super(key: key);

  @override
  _RegisterPageState createState() => _RegisterPageState();
}

class _RegisterPageState extends State<RegisterPage> {
  bool _registerEnable = false;
  TextEditingController _nameCt = TextEditingController();
  TextEditingController _passwordCt = TextEditingController();
  TextEditingController _nickNameCt = TextEditingController();


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.transparent,
        elevation: 0,
        leading: IconButton(
          color: Theme.of(context).iconTheme.color,
          icon: Icon(Icons.arrow_back_ios_new),
          onPressed: ()=>Navigator.pop(context),
        ),
        automaticallyImplyLeading: true,
      ),
      body: SingleChildScrollView(
        child: Container(
          padding: EdgeInsets.only(left: R.dimen.contentPadding,top: 60,right: R.dimen.contentPadding),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            mainAxisAlignment: MainAxisAlignment.start,
            mainAxisSize: MainAxisSize.max,
            children: [
              Text("注册 HomeStation 账号",
                style: Theme.of(context).textTheme.headline5?.copyWith(fontWeight: FontWeight.bold),
              ),
              Padding(padding: EdgeInsets.only(top: R.dimen.dimen8),
                child: Text("输入用户名 密码进行注册",
                  style: Theme.of(context).textTheme.caption?.copyWith(fontWeight: FontWeight.bold),
                ),
              ),
              Padding(padding: EdgeInsets.only(top: R.dimen.maxTopMargin),
                child: input(label: "用户名", hintText: "请输入用户名",controller: _nameCt,onChanged: onChanged),
              ),
              Padding(padding: EdgeInsets.only(top: R.dimen.dimen24),
                child: input(label: "昵称", hintText: "请输入昵称",keyboardType: TextInputType.text,controller: _nickNameCt,onChanged: onChanged),
              ),
              Padding(padding: EdgeInsets.only(top: R.dimen.dimen24),
                child: input(label: "密码", hintText: "请输密码",controller: _passwordCt,onChanged: onChanged),
              ),
              Padding(padding: EdgeInsets.only(top: R.dimen.maxTopMargin),
                child: MaterialButton(
                    textColor: Colors.white,
                    color: Theme.of(context).primaryColor,
                    shape: const RoundedRectangleBorder(
                        borderRadius: BorderRadius.all(Radius.circular(40))
                    ),
                    disabledColor: Colors.grey.shade400,
                    minWidth: double.infinity ,
                    child: const Text("注册并登陆"),
                    onPressed: _registerEnable?register:null
                ),
              ),
              Align(
                alignment: Alignment.bottomCenter,
                child: Padding(padding:EdgeInsets.only(top: 100),
                  child: Text.rich(TextSpan(
                      children: [
                        TextSpan(text: "确认注册即代表已阅读并同意",style: Theme.of(context).textTheme.caption),
                        WidgetSpan(
                          child: InkWell(
                            child: Text("《软件服务协议》",
                                style: Theme.of(context).textTheme.caption?.copyWith(color: Theme.of(context).primaryColor)
                            ),
                          ),
                        )
                      ]
                  )),
                ),
              )
            ],
          ),
        ),
      ),
    );
  }

  ///注册功能
  register(){
    var name = _nameCt.value.text.trim();
    var password = _passwordCt.value.text.trim();
    var nickName = _nameCt.value.text.trim();
    NetWork.getInstance().post("/register",
      queryParameters: {
        "name": name,
        "password":password,
        "nickName":nickName
      }
    ).then((value){
      logger.info(value);
      if(value.code == SUCCESS_CODE){
        Navigator.pop(context,true);
      }else{
        ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(value.message)));
      }
    });
  }

  onChanged(String changed){
    var name = _nameCt.value.text.trim();
    var password = _passwordCt.value.text.trim();
    var nickName = _nameCt.value.text.trim();
    setState(() {
      _registerEnable = name.length > 3 && password.length > 5 && nickName.isNotEmpty;
    });
  }

  Widget input({
    required String label,
    required String hintText,
    bool obscureText = false,
    Widget? suffix,
    ValueChanged<String>? onChanged,
    TextEditingController? controller,
    TextInputType? keyboardType,
  }){
    return Builder(builder: (context){
      return TextField(
        controller: controller,
        onChanged: onChanged,
        obscureText: obscureText,
        keyboardType: keyboardType ?? TextInputType.visiblePassword,
        decoration: InputDecoration(
            suffix: suffix,
            labelStyle: TextStyle(fontWeight: FontWeight.bold),
            floatingLabelBehavior: FloatingLabelBehavior.always,
            focusedBorder: defaultInputBorder(),
            focusedErrorBorder: defaultInputBorder(),
            enabledBorder: defaultInputBorder(),
            contentPadding: const EdgeInsets.only(left: 8,right: 8),
            labelText: label,
            hintText: hintText
        ),
      );
    });
  }

  InputBorder defaultInputBorder(){
    return UnderlineInputBorder(
        borderSide: BorderSide(width: 0.5,color: Theme.of(context).dividerColor)
    );
  }
}
